package com.awspaass.user.apps.restaurant;

import java.util.List;
import java.util.Map;

//import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.controller.MsgNoticeController;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
/**
 * 
 * @author zhangchunkui
 * 食堂管理员取消产品预订给用户发消息
 */
public class ProductReservationSendMsgBeforeLeave extends InterruptListener implements InterruptListenerInterface {
	
	@Override
	public String getDescription() {
        return "食堂管理员取消产品预订给用户发消息！";
    }
	
	@Override
	public boolean execute(ProcessExecutionContext pec) throws Exception {
		boolean flag_cancel = SDK.getTaskAPI().isChoiceActionMenu(pec.getTaskInstance(), "预订取消");
		if(flag_cancel) {
			try {
				ProcessInstance processInstance = pec.getProcessInstance();
				String bindId = processInstance.getId();//流程实例ID
				String target = pec.getTaskInstance().getTarget();//节点任务办理人
				String guanliyuan =pec.getUserContext().getUserModel().getUserName();//办理人名称
				String telephone =pec.getUserContext().getUserModel().getMobile();//节点办理人电话
				String orderSql = "select id,applyuid,orderdate,area,getfoodtime,period from BO_EU_SHSYST_FOODORDER_HEAD where bindid='"+bindId+"' ";
				List<Map<String, Object>> dataList = DBSql.query(orderSql, new ColumnMapRowMapper(), new Object[] {});
				if (dataList == null || dataList.isEmpty()) {
					throw new BPMNError("未查询到订单，消息取消通知失败！");
				}
				Map<String, Object> dataMap = dataList.get(0);
				String applyUid = CoreUtil.objToStr(dataMap.get("applyuid"));//预订人id
				String area = CoreUtil.objToStr(dataMap.get("area"));//取货食堂
				String getfoodtime = CoreUtil.objToStr(dataMap.get("getfoodtime"));//取货时间
				String areaName = CoreUtil.objToStr(DBSql.getString("select item.cnname areaname from Bo_Act_Dict_Kv_Item item "
						+ "left join BO_ACT_DICT_KV_MAIN main on item.bindid = main.bindid "
						+ "where substr('"+area+"',0,2)=item.itemno and main.dictkey = 'shdininghallarea'", "areaname"));			
				String applyName = SDK.getORGAPI().getUser(applyUid).getUserName();
				String applyMobile = SDK.getORGAPI().getUser(applyUid).getMobile();
				//发消息
				String msg ="尊敬的"+applyName+"，您预订"+getfoodtime+areaName+"的外卖食品，因无库存而无法进行预订。给您带来不便敬请谅解，谢谢！如有任何疑问可联系："+
				guanliyuan+"，电话："+telephone+"。";
				MsgNoticeController.sendNoticeMsg(UserContext.fromUID(target), msg,"admin", applyUid, "1", "");
				//发短信
				String param = "{'CONTACTPERSON':'"+applyName+"','YCDATEAREA':'"+getfoodtime+areaName+
						"','GUANLIYUAN':'"+guanliyuan+"','TELEPHONE':'"+telephone+"'}";
				SmsUtil.sendSms(applyMobile,"SMS_256000119",param);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	
}
